package com.seari.ibp;

import com.seari.utils.FXHelper;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FXMLController implements Initializable
{
    
    @FXML
    private Label label;
    
    @FXML
    private Label leftlabel;
    
    @FXML
    private Label timelabel;
    
    @FXML
    private ToggleGroup group1;
    
    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException
    {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }
    
    @FXML
    private void rightlabelClick() throws IOException
    {
        System.out.println("com.seari.ibp.FXMLController.rightlabelClick()");
        Stage stage = FXHelper.getStageByName("main");
        Scene scene = FXHelper.getSceneByName("right");
        stage.setScene(scene);
        stage.show();
    }
    
    private void handleGroup1Event()
    {
        
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
        System.out.println("com.seari.ibp.FXMLController.initialize()");
        initClock();
        //添加按钮组监听
        group1.selectedToggleProperty().addListener((ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) ->
        {
            ToggleButton tb = (ToggleButton)group1.getSelectedToggle();
            if(tb!= null)
            {
                System.out.println(((ToggleButton)group1.getSelectedToggle()).getId());
            }
        });
    }    
    
    private void initClock()
    {
        Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, (ActionEvent event) ->
        {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            timelabel.setText(LocalDateTime.now().format(formatter));
        }), new KeyFrame(Duration.seconds(1)));
        clock.setCycleCount(Animation.INDEFINITE);
        clock.play();
    }
}
